﻿#pragma once
#include<iostream>
//#include<vector>

using namespace std;
//实现底层的红黑树结构

enum Coler{RED,BLACK};

//设置红黑树的节点
template<class T>
struct RBtreeNode
{
	T _date;

	//在红黑树中设置三个指针，分别指向父亲，左右节点
	RBtreeNode<T>* _parent;
	RBtreeNode<T>* _right;
	RBtreeNode<T>* _left;

	//设置红黑树的颜色
	Coler _coler;

	RBtreeNode(T date)
		:_date(date)
		,_parent(nullptr)
		,_right(nullptr)
		,_left(nullptr)
		,_coler(BLACK)
	{}

};

template<class T,class Ref,class Ptr>
struct RBTreeIterator
{
	typedef RBtreeNode<T> Node;
	typedef RBTreeIterator<T, Ref, Ptr> self;

	Node* _node;
	Node* _root;
	RBTreeIterator(Node* node,Node* root)
		:_node(node)
		,_root(root)
	{}
	//前置++
	self operator ++()
	{
		if (_node->_right)
		{
			Node* cur = _node->_right;
			while (cur && cur->_left)cur = cur->_left;
			_node = cur;
		}
		else
		{
			Node* cur = _node;
			Node* parent = cur->_parent;

			while (parent && cur == parent->_right)cur = parent,parent = cur->_parent;
			_node = parent;
		}
		return *this;
	}

	self operator ++(int)
	{
		Node* cur = _node;
		++_node;
		return cur;
	}

	self operator --()
	{
		if (_node == nullptr)
		{
			//走到最大值去
			Node* Maxcur = _root;

			while (Maxcur && Maxcur->_right)
			{
				Maxcur = Maxcur->_right;
			}
			_node = Maxcur;
		}
		else if (_node->_left)
		{
			Node* cur = _node->_left;
			while (cur && cur->_right)cur = cur->_right;
			_node = cur;
		}
		else
		{
			Node* cur = _node;
			Node* parent = cur->_parent;
			while (parent && parent->_left == cur)cur = parent, parent = cur->_parent;
			_node = parent;
		}
		return *this;
	}

	Ref operator*()
	{
		return _node->_date;
	}

	Ptr operator->()
	{
		return &(_node->_date);
	}

	bool operator !=(const self&s)
	{
		return _node != s._node;
	}

	bool operator== (const self& s)
	{
		return _node == s._node;
	}


};

//设置红黑树
template<class K,class T,class KofT>
class RBTree
{
public:
	//using充当重命名的作用
	using Node = RBtreeNode<T>;
	typedef RBTreeIterator<T,T&,T*> Iterator;
	typedef RBTreeIterator<T,T&,T*> ConstIterator;
	//using const_Iterator = RBTreeIterator<T,const T&,const T*>;

public:

	Iterator Begin()
	{
		Node* cur = _root;
		while (cur && cur->_left)
		{
			cur = cur->_left;
		}

		return Iterator(cur,_root);
	}

	Iterator End()
	{
		return Iterator(nullptr,_root);
	}

	ConstIterator Begin()const
	{
		Node* cur = _root;
		while (cur && cur->_left)
		{
			cur = cur->_left;
		}

		return ConstIterator(cur,_root);
	}

	ConstIterator End()const
	{
		return ConstIterator(nullptr,_root);
	}

	//完成第一个方法：插入Insert
	pair<Iterator,bool> Insert(const T date)
	{
		if (_root == nullptr)
		{
			//开始是空树的情况
			_root = new Node(date);
			_root->_coler = BLACK;
			return {Iterator(_root,_root),true};
		}

		//不是空树的情况
		//首先要找到要插入的节点位置
		Node* cur = _root;
		Node* parent = _root;

		KofT kot;//实例化KofT,为实现只进行K比较
		while (cur)
		{
			//比较键值，选择要往那边走
			if (kot(cur->_date) < kot(date))
			{
				parent = cur;
				cur = cur->_right;
			}
			else if (kot(cur->_date) > kot(date))
			{
				parent = cur;
				cur = cur->_left;
			}
			else
			{
				//相同不允许插入，实现有去重功能的底层红黑树
				cout << "相同元素，插入失败";
				return { Iterator(cur,_root),false };
			}
		}

		//走到这，就代表已经找到位置，可以进行插入
		//此时cur指向的节点为空，但是parent指向的节点就是要插入的位置的父节点
		cur = new Node(date);
		Node* newnode = cur;
		cur->_coler = RED;
		if (kot(parent->_date) > kot(date))
			parent->_left = cur;
		else
			parent->_right = cur;
		cur->_parent = parent;
		//插入成功
		//要开始调整

		//新加入的节点为红，且其父节点也是红，并且爷爷节点为肯定为黑，(与第三条：不能出现连续的红色节点；冲突)
		while (parent && parent->_coler == RED)
		{
			//先要找到爷爷节点
			Node* Grandparent = parent->_parent;

			//讨论叔叔节点的位置,如果叔叔节点在爷爷节点的右边
			if (Grandparent->_left == parent)
			{
				//第一种情况，
				//当且叔叔节点，不为空，且为黑，就是第一种情况

				//只需要变色，无需旋转
				//爷爷变红，父亲和叔叔节点一起变黑即可
				Node* uncle = Grandparent->_right;
				if (uncle && uncle->_coler == RED )
				{
					Grandparent->_coler = RED;
					parent->_coler = uncle->_coler = BLACK;

					//还要继续向上便利
					cur = Grandparent;
					parent = cur->_parent;
				}
				else 
				{
					//第二种情况：叔叔节点(不存在或者为黑)
					//如果新插入的节点是在父亲的左边，那就需要单旋这+变色即可
					if (cur == parent->_left)
					{
						RotateR(Grandparent);
						Grandparent->_coler = RED;
						parent->_coler = BLACK;
					}
					else
					{
						//这时新插入的节点在父亲的右边,那就需要左右双旋+变色
						RotateL(parent);
						RotateR(Grandparent);

						cur->_coler = BLACK;
						Grandparent->_coler = RED;
					}
					break;
				}
			}
			else if (Grandparent->_right == parent)
			{
				//讨论叔叔节点的位置,如果叔叔节点在爷爷节点的左边
				
				//当插入的位置在是父节点的右边的时候
				//同样需要判断第一种情况
				Node* uncle = Grandparent->_left;
				if (uncle && uncle->_coler == RED )
				{
					Grandparent->_coler = RED;
					parent->_coler = uncle->_coler = BLACK;

					//还要继续向上便利
					cur = Grandparent;
					parent = cur->_parent;
				}
				else 
				{
					//第二种情况：讨论的是叔叔节点(不存在或者为黑)
					//此时需要左旋+变色（将爷爷变红，父亲变黑）
					if (cur == parent->_right)
					{
						RotateL(Grandparent);
						Grandparent->_coler = RED;
						parent->_coler = BLACK;
					}
					else
					{
						RotateR(parent);
						RotateL(Grandparent);

						Grandparent->_coler = RED;
						cur->_coler = BLACK;
					}
					break;
				}
			}
			else
			{
				return {Iterator(newnode,_root),false};
			}
		}

		//最后无论什么情况，都将根节点变为黑即可
		_root->_coler = BLACK;

		//调整完成
		return { Iterator(newnode,_root),true };
	}

	//左旋代码
	void RotateL(Node* spin)
	{
		// 先将连着 spin 的上个节点，存储一下
		Node* parent = spin->_parent;
		// 要变成新的根
		Node* stem = spin->_right;
		// 要改变位置的节点
		Node* stemLeft = stem->_left;

		// 将 stem 的左子节点连接到 spin 的右子节点位置
		spin->_right = stemLeft;
		if (stemLeft)
			stemLeft->_parent = spin;

		// 将 spin 作为 stem 的左子节点
		stem->_left = spin;
		spin->_parent = stem;

		// 如果 spin 是根节点，更新根节点为 stem
		if (spin == _root)
		{
			_root = stem;
			stem->_parent = nullptr;
		}
		else
		{
			// 判断 spin 是其父节点的左子节点还是右子节点
			if (parent->_left == spin)
				parent->_left = stem;
			else
				parent->_right = stem;

			// 更新 stem 的父节点为 spin 的原父节点
			stem->_parent = parent;
		}
	}

	//右旋代码
	void RotateR(Node* spin)
	{
		// 先将连着 spin 的上个节点，存储一下
		Node* parent = spin->_parent;
		// 要变成新的根
		Node* stem = spin->_left;
		// 要改变位置的节点
		Node* stemRight = stem->_right;

		// 将 stem 的右子节点连接到 spin 的左子节点位置
		spin->_left = stemRight;
		if (stemRight)
			stemRight->_parent = spin;

		// 将 spin 作为 stem 的右子节点
		stem->_right = spin;
		spin->_parent = stem;

		// 如果 spin 是根节点，更新根节点为 stem
		if (spin == _root)
		{
			_root = stem;
			stem->_parent = nullptr;
		}
		else
		{
			// 判断 spin 是其父节点的左子节点还是右子节点
			if (parent->_left == spin)
				parent->_left = stem;
			else
				parent->_right = stem;

			// 更新 stem 的父节点为 spin 的原父节点
			stem->_parent = parent;
		}
	}

	Node* Find(const T date)
	{
		Node* cur = _root;
		KofT kot;
		while (cur)
		{
			if (kot(cur->_date) > kot(date))cur = cur->_left;
			else if (kot(cur->_date) < kot(date))cur = cur->_right;
			else
			{
				//cout << "找到了" << endl;
				return cur;
			}
		}
		return nullptr;
	}

	/*void Inorther()
	{
		_Inorther(_root);
	}*/

	~RBTree()
	{
		Destory(_root);
		_root = nullptr;
	}

	void Destory(Node* root)
	{
		if (root == nullptr)
			return;

		Destory(root->_left);
		Destory(root->_right);
		delete root;
	}

	int Height()
	{
		return _Height(_root);
	}

private:
	int _Height(Node* root)
	{
		if (root == nullptr)
			return 0;
		int leftHeight = _Height(root->_left);
		int rightHeight = _Height(root->_right);
		return leftHeight > rightHeight ? leftHeight + 1 : rightHeight + 1;
	}

	/*void _Inorther(Node* root)
	{
		if (root == nullptr)
			return;

		_Inorther(root->_left);
		cout << root->_kv.first << ":" << root->_kv.second << endl;
		_Inorther(root->_right);
	}*/

	Node* _root = nullptr;
};
